package com.levi.orderly;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * 顺序消息消费者
 * 重点:  声明注册监听器 声明成  MessageListenerOrderly
 */
public class OrderlyConsumer {
    public static void main(String[] args) throws Exception {
        //1.创建消费者，指定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        //2.指定nameserv地址
        consumer.setNamesrvAddr("192.168.177.129:9876");
        //3.订阅topic和tag
        consumer.subscribe("OrderTopic","*");
        //4.注册消费监听器
        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
                for (MessageExt msg : msgs) {
                    System.out.println("当前线程名称: " + Thread.currentThread().getName() + " 消费消息 :" + new String(msg.getBody()));
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        consumer.start();w
        System.out.println("消费者启动");
    }

}
